/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.menu.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.persistence.ResponseResult;
import com.thinkgem.jeesite.modules.menu.entity.OrderGoods3;
import com.thinkgem.jeesite.modules.menu.service.OrderGoods3Service;
import com.thinkgem.jeesite.modules.shop.entity.Order;
import com.thinkgem.jeesite.modules.shop.service.OrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.menu.entity.Order3;
import com.thinkgem.jeesite.modules.menu.service.Order3Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单Controller
 * @author dby
 * @version 2020-07-12
 */
@Controller
@RequestMapping(value = "${adminPath}/menu/order3")
public class Order3Controller extends BaseController {

	@Autowired
	private Order3Service order3Service;

	@Autowired
	private OrderGoods3Service orderGoods3Service;


	@Autowired
	private OrderService orderService;
	
	@ModelAttribute
	public Order3 get(@RequestParam(required=false) String id) {
		Order3 entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = order3Service.get(id);
		}
		if (entity == null){
			entity = new Order3();
		}
		return entity;
	}
	
	@RequiresPermissions("menu:order3:view")
	@RequestMapping(value = {"list",""})
	public String list(Order3 order3, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Order3> page = order3Service.findPage(new Page<Order3>(request, response), order3); 
		model.addAttribute("page", page);
		return "modules/menu/order3List";
	}

	@RequiresPermissions("menu:order3:view")
	@RequestMapping(value = "form")
	public String form(Order3 order3, Model model) {

		List<OrderGoods3> orderGoodsList = null;

		//新增时  order id 为空
		if(StringUtils.isBlank(order3.getId())){

		}else{
			OrderGoods3 orderGoods3 =  new OrderGoods3();
			orderGoods3.setOrderId(order3.getId());
			orderGoodsList = orderGoods3Service.findList(orderGoods3);
		}

		if (null == orderGoodsList || orderGoodsList.isEmpty()) {
			orderGoodsList = new ArrayList<>();
			orderGoodsList.add(new OrderGoods3());
		}
		model.addAttribute("order3", order3);
		model.addAttribute("orderGoodsList", orderGoodsList);

		return "modules/menu/order3Form";
	}

	@RequiresPermissions("menu:order3:edit")
	@RequestMapping(value = "save")
	public String save(Order3 order3, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, order3)){
			return form(order3, model);
		}
		try {
			order3Service.saveOrder(order3);
			addMessage(redirectAttributes, "保存订单成功");
		} catch (Exception e) {
			e.printStackTrace();
			addMessage(redirectAttributes, "保存订失败");
		}
		return "redirect:"+Global.getAdminPath()+"/menu/order3/?repage";
	}
	
	@RequiresPermissions("menu:order3:edit")
	@RequestMapping(value = "delete")
	public String delete(Order3 order3, RedirectAttributes redirectAttributes) {
		order3Service.delete(order3);
		addMessage(redirectAttributes, "删除订单成功");
		return "redirect:"+Global.getAdminPath()+"/menu/order3/?repage";
	}



	@RequiresPermissions("menu:order3:view")
	@RequestMapping(value = {"myOrderlist"})
	public String list1(Order3 order3, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Order3> page = order3Service.findPage(new Page<Order3>(request, response), order3);
		model.addAttribute("page", page);
		return "modules/menu/myOrder3List";
	}


	/*
	 * @Author Zhang Xingliang
	 * @Description 下单 在orderService 去完成 订单保存 及订单商品保存 一般 使用本方法 形式
	 *
	 *  本方法 前端html页面 需要使用ajax 方式提交表单 传入 order对象
	 *
	 * @Date 22:38 2020/7/7
	 **/
	@RequestMapping(value = "saveOrder1")
	@ResponseBody
	public ResponseResult save1(@RequestBody Order order) {
		orderService.save(order);
		return ResponseResult.success();
	}



}